Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule Change: Suggestion fixer for prefer-destructuring #18460

Open
1 task
Samuel-Therrien-Beslogic opened this issue May 16, 2024 · 3 comments
Open
1 task
Labels
3rd party plugin This is an issue related to a 3rd party plugin, config, or parser

Comments

@Samuel-Therrien-Beslogic
Copy link

Samuel-Therrien-Beslogic commented May 16, 2024

What rule do you want to change?

prefer-destructuring

What change do you want to make?

Implement suggestions

How do you think the change should be implemented?

A new default behavior

Example code

const selectedCost = costs[index_ - 1]

image

What does the rule currently do for this code?

Warn but doesn't offer fixes

What will the rule do after it's changed?

Offer a suggestion to manually fix:

const {[index_ - 1]: selectedCost} = costs

Participation

  • I am willing to submit a pull request to implement this change.

Additional comments

My original use-case was actually for @typescript-eslint/prefer-destructuring. If I understand correctly, if this is added for ESLint, typescript-eslint should follow with their support.

From:

export abstract class Question {
  readonly id?: number
  readonly text: string

  constructor(data: {
    readonly id?: number,
    readonly text: string,
  }) {
    this.id = data.id
    this.text = data.text
  }
}

To:

export abstract class Question {
  readonly id?: number
  readonly text: string

  constructor(data: {
    readonly id?: number,
    readonly text: string,
  }) {
    ({ id: this.id, text: this.text } = data)
  }
}

The parenthesis requirement make the enforceForRenamedProperties option hard for me to enable as I feel like other team members will also get confused as to why the syntax is incorrect until realizing that parenthesis are required w/o assignement keywords.

@Samuel-Therrien-Beslogic Samuel-Therrien-Beslogic added enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules labels May 16, 2024
@aladdin-add
Copy link
Member

the plugin is not maintained by eslint team. please file it in https://github.com/typescript-eslint/typescript-eslint.

@aladdin-add aladdin-add added 3rd party plugin This is an issue related to a 3rd party plugin, config, or parser and removed enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules labels May 16, 2024
@Samuel-Therrien-Beslogic Samuel-Therrien-Beslogic changed the title Rule Change: Suggestion fixer for @typescript-eslint/prefer-destructuring Rule Change: Suggestion fixer for prefer-destructuring May 18, 2024
@Samuel-Therrien-Beslogic
Copy link
Author

Samuel-Therrien-Beslogic commented May 18, 2024

@aladdin-add I did mean prefer-destructuring and accidentally left in @typescript-eslint. I've updated the post accordingly. Could you please reopen?

@aladdin-add aladdin-add reopened this May 18, 2024
@Tanujkanti4441
Copy link
Contributor

Hi @Samuel-Therrien-Beslogic,

const selectedCost = costs[index_ - 1]
fixes to
const {[index_ - 1]: selectedCost} = costs

this example is a bit confusing, while const selectedCost = costs[index_ - 1]; accesses an array element directly, the fix const {[index_ - 1]: selectedCost} = costs; suggests object destructuring, which is not applicable to arrays
can you please add some code example you want to have suggestions for with appropriate fixes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3rd party plugin This is an issue related to a 3rd party plugin, config, or parser
Projects
Status: Evaluating
Development

No branches or pull requests

3 participants